package com.ysyc.driver_ee.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.adapter.CapacityOrderFromAdapter;
import com.ysyc.driver_ee.adapter.OrderCompletedAdapter;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.factory.RecyclerViewFactory;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.model.OrderForm;
import com.ysyc.driver_ee.tools.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/3 13:25<br>
 * 版本： v1.0<br>
 */
public class CapacityOrderFromFragment extends BaseFragment {
    @BindView(R.id.xrv_capacity_view)
    RecyclerView rvCapacityView;

    private NetworkCallback<List<DriverInfo>> listNetworkCallback;
    private int type;
    public CapacityOrderFromAdapter adapter ;




    @Override
    protected int getLayoutId() {
        return R.layout.fragment_capacity_orderfrom;
    }


    @Override
    protected void onFragmentCreate(View view) {
        super.onFragmentCreate(view);
        type = args.getInt(CapacityFragment.TYPE_KEY);
        RecyclerViewFactory.initVerticalRvDecoration(getContext(), rvCapacityView, 10);
         adapter = new CapacityOrderFromAdapter(getActivity(),R.layout.xrv_capacity_distribution_item);
        rvCapacityView.setAdapter(adapter);
        initView();
    }

    private void initView() {
        initNetworkRequest();
        if (type ==0){
            getOrderFormList();
        }
    }

    private void initNetworkRequest() {
        listNetworkCallback = new NetworkCallback<>(new HttpResource<List<DriverInfo>>() {
            @Override
            public void onSuccess(List<DriverInfo> driverInfo) {
                adapter.bindingList(driverInfo);
            }

            @Override
            public void onFailure(String errorDes) {
                ToastUtils.show(errorDes);
            }

            @Override
            public void onLoading(Dialog dialog) {

            }
        });
    }


    public void getOrderFormList() {
        Observable<List<DriverInfo>> orderFormItemDriverListObservable = RetrofitClient.getInstance().getHttpServices().getDriverList( type);
        listNetworkCallback.postRequest(orderFormItemDriverListObservable, null);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            getOrderFormList();
        }
    }



}
